package com.suncaper.shop.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.suncaper.shop.domain.Category;
import com.suncaper.shop.domain.Product;
import com.suncaper.shop.service.CategoryService;
import com.suncaper.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class ShopController {
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping(value = {"/shop_right_sidebar.html", "/shop"})
    public String goToShop(Model model, HttpSession session) {



        if (!model.containsAttribute("all")) {
            List<Product> all = productService.findAll();
            model.addAttribute("all", all);
        }
        model.addAttribute("allhot", productService.findAllByHot());
        List<Category> categoryList = categoryService.findAllCategory();
        session.setAttribute("shopList", categoryList);

        return "shop";
    }


    @RequestMapping("/shop/list")
    public String findByfactor(@RequestParam(name = "fact", required = false, defaultValue = "") String fact,
                               @RequestParam(name = "direct", required = false, defaultValue = "desc") String direct,
                               Map<String, Object> map) {
        map.put("all", productService.findAll());
        map.put("allhot", productService.findAllByHot());
        if (!fact.equals("")) {
            List<Product> productList = productService.findByFactor(fact, direct);
            map.put("all", productList);
            map.put("active", fact);
        }
        System.out.println(fact);
        return "shop";
    }


    public String findByPage(HttpServletRequest request, HttpServletResponse response) {
        return "shop";
    }


    @PostMapping("/shop/filter")
    public String filter(HttpServletRequest request, Map<String, Object> map, HttpSession session) {
        String min_price = request.getParameter("min_price");
        String max_price = request.getParameter("max_price");
        session.setAttribute("min", min_price);
        session.setAttribute("max", max_price);
        System.out.println(min_price + ':' + max_price);

        List<Product> productList = productService.FilterByMoney(Double.parseDouble(min_price), Double.parseDouble(max_price));
        map.put("all", productList);
        return "shop";

    }


    @PostMapping("/product/search")
    public String search(@RequestParam(value = "search_category", required = false) Integer cid, @RequestParam("s") String key, Model model) throws IOException {

      //  if(cid !=null){
            model.addAttribute("all", productService.findByname(key, cid));
       // return "shop";
       // }
       // model.addAttribute("all",productService.elasticSearch(key));
//
        return "shop";
    }

    @GetMapping("/shop/{cid}")
    public String findByCategory(@PathVariable("cid") Integer cid, Model model, HttpSession session) {

        model.addAttribute("all", productService.findAllByCid(cid));
        List<Category> categoryList = categoryService.findAllCategory();
        session.setAttribute("shopList", categoryList);
        return "shop";
    }


}
